package com.aiatomic.couting.account;

import com.aiatomic.couting.AccountInfoActivity;
import com.aiatomic.couting.IConstant;
import com.aiatomic.couting.LoginActivity;
import com.aiatomic.couting.R;
import com.aiatomic.couting.util.AppUtils;

import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Toast;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

public class FragmentAccountInfoMain extends Fragment {
	private View view;
	private AccountInfoActivity activity;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_account_info_main, container, false);
		activity = (AccountInfoActivity) getActivity();
		bindEvent();
		return view;
	}

	private void bindEvent() {
		view.findViewById(R.id.buy).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				FragmentAccountInfoShop fragment = new FragmentAccountInfoShop();
				activity.addFragment(fragment);
			}
		});
		view.findViewById(R.id.orders).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				FragmentAccountInfoOrders fragment = new FragmentAccountInfoOrders();
				activity.addFragment(fragment);
			}
		});
		view.findViewById(R.id.feedback).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
//				FragmentAccountInfoFeedback fragment = new FragmentAccountInfoFeedback();
//				activity.addFragment(fragment);
			}
		});
		view.findViewById(R.id.safe_exit).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(AppUtils.isLogin(getActivity())){
					if(AppUtils.channel==IConstant.CHANNEL_QQ){
						Platform platform = ShareSDK.getPlatform(QQ.NAME);
						platform.removeAccount(true);
					}
					if(AppUtils.channel==IConstant.CHANNEL_WX){
						Platform platform = ShareSDK.getPlatform(Wechat.NAME);
						platform.removeAccount(true);
					}
					if(AppUtils.channel==IConstant.CHANNEL_SINA){
						Platform platform = ShareSDK.getPlatform(SinaWeibo.NAME);
						platform.removeAccount(true);
					}
					AppUtils.clean();
					SharedPreferences sharedPreferences = activity.getSharedPreferences("safe_login", activity.MODE_PRIVATE);
					SharedPreferences.Editor editor=sharedPreferences.edit().clear();
					editor.commit();
					
				}
				if(!AppUtils.isLogin(getActivity())){
					Intent intent = new Intent(getActivity(), LoginActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					getActivity().startActivity(intent);
					Toast toast = Toast.makeText(getActivity(), getString(R.string.safe_exit_done), Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				}
			}
		});
	}
}
